package 笔试题.博乐科技;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Stack;

public class 进制转换 {
    public String sixToThree (String sixString) {
        HashMap<Character, Integer> dic = new HashMap<>();
        dic.put('0', 0);
        dic.put('1', 1);
        dic.put('2', 2);
        dic.put('3', 3);
        dic.put('4', 4);
        dic.put('5', 5);
        dic.put('6', 6);
        dic.put('7', 7);
        dic.put('8', 8);
        dic.put('9', 9);
        dic.put('A', 10);
        dic.put('B', 11);
        dic.put('C', 12);
        dic.put('D', 13);
        dic.put('E', 14);
        dic.put('F', 15);
        char[] number = sixString.substring(2).toCharArray();
        //转换为10进制
        int decimal = 0;
        for (int i = 0; i < number.length; i++) {
            decimal = decimal * 16 + dic.get(number[i]);
        }
        //转换为三进制
        Deque<Integer> stack = new ArrayDeque<>();
        while (decimal > 0) {
            stack.push(decimal % 3);
            decimal /= 3;
        }
        StringBuilder res = new StringBuilder();
        while (!stack.isEmpty()) {
            res.append(stack.pop());
        }
        return res.toString();
    }
}
